#include "key.h"

uint8_t Key_read(void)
{
    if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0) == 0)
        return  1;
    else if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == 0)
        return  2;
    else if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == 0)
        return  3;
    else if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0)
        return  4;
    else
        return 0;
}



uint8_t key_now, key_up, key_down, key_old,key_up;
uint32_t key_long_time;//长按计时
uint8_t out_lock;//1上锁
uint32_t out_lock_time;//上锁时间
uint8_t set_select;//参数选择0R | 1K
uint8_t true_R = 1, true_K = 1;//真正用于计算的R和K
uint8_t duty_lock;//占空比上锁功能，1为上锁
void Key_proc(void)
{
    
    key_now = Key_read();
    key_down = key_now & (key_now ^ key_old);
    key_up = ~key_now & (key_now ^ key_old);
    key_old = key_now;

    if(key_down)
	{
        key_long_time = HAL_GetTick();
        switch(key_down)
		{
			case 1:
                show_mode++;
                if(show_mode>2)
                    show_mode = 0;

                if(show_mode == 1)
                {
                    set_select = 0;
                }
                if(show_mode == 2)
                {
                    true_R = set_R;
                    true_K = set_K;
                }
                break;
            case 2:
                if (show_mode == 0 && !out_lock)
                {
                    // if(out_mode == 'L')
                    // {
                    //     out_mode == 'H';
                    // }
                    // else if (out_mode == 'H')
                    // {
                    //     out_mode == 'L';
                    // }
                    out_lock = 1;
                    out_lock_time = HAL_GetTick();
                    led_100ms_time = HAL_GetTick();
                }

                if(show_mode == 1)
                {
                    set_select ^= 1;
                }

                break;
                //+
            case 3:
                if(show_mode == 1)
                {
                    if(set_select == 0)
                    {
                        set_R++;
                        if (set_R > 10)
                            set_R = 1;
                    }
                    else if(set_select == 1)
                    {
                        set_K++;
                        if(set_K>10)
                            set_K = 1;
                    }
                }

                break;
                //-
            case 4:
                if (show_mode == 1)
                {
                    if (set_select == 0)
                    {
                        set_R--;
                        if (set_R <1)
                            set_R = 10;
                    }
                    else if (set_select == 1)
                    {
                        set_K--;
                        if (set_K <1)
                            set_K = 10;
                    }
                }
                else if(show_mode == 0)
                {
                    duty_lock = 0;
                }
                break;
        }
	}
    uint32_t now_tim = HAL_GetTick();
    if (key_up && (now_tim >= key_long_time + 2000))
    {
        key_long_time = 0;
        switch(key_up)
        {
			case 4:
            if (show_mode == 0)
                duty_lock = 1;
			break;
			
			default:break;
        }
    }
}

